﻿<cfsilent>
	<cfsetting requesttimeout="90" />
	<cfscript>

		event.setArg("pageTitle", "网上选课 - 计划课程");


		sessionAdvice = getProperty("serviceFactory").getBean("sessionAdvice");
		dictAdvice = getProperty("serviceFactory").getBean("senateDictionaryAdvice");
		datetimeAdvice = getProperty("serviceFactory").getBean("datetimeAdvice");
		stringAdvice = getProperty("serviceFactory").getBean("stringAdvice");
		
		chooseProp = getProperty("serviceFactory").getBean("choosePropertyAdvice");
		taskProp = getProperty("serviceFactory").getBean("taskPropertyAdvice");
		turnProp = getProperty("serviceFactory").getBean("turnPropertyAdvice");

		termId = event.getArg("TermID");

		academicYear = event.getArg("AcademicYear");
		academicTerm = event.getArg("AcademicTerm");

		/* 学籍相关信息 */
		studentId = sessionAdvice.getAutherUserID();
		studentClassId = sessionAdvice.getUserProp("studentClass");
		studentSubjectId = sessionAdvice.getUserProp("studentSubject");
		studentDirId = sessionAdvice.getUserProp("studentSubDir");
		studentGrade = sessionAdvice.getUserProp("studentGrade");
		studentDepartment = sessionAdvice.getUserProp("studentDepartment");
		
		currentTab = event.getArg("TabID", "labCourse");
		
		/* 读取学生的已选课程 */
		
		sql = "SELECT
					c.cid,c.course_name,c.course_credit,
					ct.turn,
					sc.task_type,sc.scc_id,sc.st,sc.retaking,
					sc.choose_time,sc.choose_prop,sc.task_id,cp.prop_name
				FROM
					t_student_course sc
					INNER JOIN t_course c ON c.cid = sc.cid
					INNER JOIN t_choose_turn ct ON ct.ct = sc.ct
					INNER JOIN t_course_prop cp ON cp.prop_id = sc.prop_id
				WHERE sc.stu_id = :studentId  
					  AND sc.term_id = :termId 
				ORDER BY
					  sc.st,c.course_name ";
		
		queryObj = new Query(datasource = application.dnsMaster);
		
		queryObj.addParam(name="studentId", value=studentId, cfsqltype="cf_sql_varchar");
		queryObj.addParam(name="termId", value=termId, cfsqltype="cf_sql_char");
		
		rs_choosen = queryObj.execute(sql = sql).getResult();

		/* 生成禁止选课课程清单 */
		courseArrayDisable = [];
		courseArrayChoosen = [];
		
		for(a = 1;a LTE rs_choosen.recordCount;a++){
			arrayAppend(courseArrayChoosen, rs_choosen['cid'][a]);
			chooseProp.parseProperty( rs_choosen['choose_prop'][a] );
			if ( chooseProp.getLocked() ) {
				arrayAppend(courseArrayDisable, rs_choosen['cid'][a]);
			}
		}
		
		turnId = event.getArg("CT");
		
		chooserAllowed = false;
		
		if ( len(turnId) ) {
			/* 再次检查选课时段和允许范围 */
			sql = " SELECT
						tct.ct,tct.date_start,tct.date_end,tct.time_start,tct.time_end,
						tct.grade_allowed,tct.institute_allowed,tct.subject_allowed,tct.turn_prop
					FROM 
						t_choose_turn tct
					WHERE
						tct.ct = :turnId ";

			queryObj = new Query(datasource = application.dnsSlave);
			
			queryObj.addParam(name="turnId", value=turnId, cfsqltype="cf_sql_char");
			
			rs_turn = queryObj.execute(sql=sql).getResult();

			

			if (rs_turn.recordCount GT 0) {
				
				today = datetimeAdvice.getCurrentDateString();
				currentTime = TimeFormat(now(), "HHMM");
				
				if ( rs_turn.date_start lte today and today lte rs_turn.date_end ) {
		
					if ( rs_turn.time_start lte currentTime and currentTime lte rs_turn.time_end ) {
						if ( rs_turn.grade_allowed eq "ALL" or listFind(rs_turn.grade_allowed, studentGrade, ",") ) {
							if ( rs_turn.institute_allowed eq "ALL" or listFind(rs_turn.institute_allowed, studentDepartment, ",") or (rs_turn.subject_allowed neq '-' and listFind(rs_turn.subject_allowed, studentSubjectId)) ) {
								/* 是否开放计划课程选课 */
								turnProp.parseProperty( rs_turn.turn_prop );
								if ( turnProp.getProgramTask() ) {
									chooserAllowed = true;
								}
							}
						}
					}
				}
			}


			if ( chooserAllowed ) {
				
				
				/* 根据班级读取计划课程信息 */
				
				/* TaskProps */
				/* 第3位 是否禁止学生选课 即老师已经开始成绩提交 或 考试安排已发布 */
				/* 第7位 是否撤班 */

				sql = "SELECT a.cid, a.course_name, a.course_credit, a.prop_id,
						a.period, a.period_theory, a.period_practice,a.period_computer,
						c.prop_name, 
						CASE
						    WHEN a.intro IS NULL THEN '-' ELSE a.intro 
						    END intro,
					    CASE 
						    WHEN a.reference_books IS NULL THEN '-' ELSE a.reference_books
						    END reference_books 
						FROM t_task b 
						INNER JOIN t_course a ON a.cid = b.cid  
						INNER JOIN t_course_prop c ON c.prop_id = a.prop_id 
						WHERE b.term_id =:termId 
						AND
						SUBSTR(b.task_prop,3,1) = '0' 
						AND
						SUBSTR(b.task_prop,7,1) = '0' 
						AND a.prop_id = '2'";
				
				queryObj = new Query(datasource = application.dnsSlave);
				
				queryObj.addParam(name="termId", value=termId, cfsqltype="cf_sql_char");
				
				rs_course = queryObj.execute(sql=sql).getResult();
				
				writeLog( text=termId );
				writeLog( text=studentClassId );
				writeLog( text='%' & studentDirId & '%' );
				
				//writeDump( rs_course );
				//abort;
					
			}
		
			
		
		}
	
	</cfscript>
</cfsilent>

<div id="headArea">
	<div class="uiHeader uiHeaderWithImage ptm">
		<div class="clearfix uiHeaderTop">
			<div class="uiHeaderActions rfloat">
				<a class="uiButton" href="<cfoutput>#buildURL('DEFAULT_EVENT')#</cfoutput>"><i class="mrs img btnPublish"></i><span class="uiButtonText">网上选课</span></a>
			</div>
			<div>
				<h2 class="uiHeaderTitle">
					<i class="uiHeaderImage img icon16x16 application"></i><cfoutput>#academicYear#</cfoutput>学年 <cfoutput>#dictAdvice.getTerm(academicTerm)#</cfoutput> - 计划课程
				</h2>
			</div>
		</div>
	</div>
</div>

<div class="UIElement">
	<ul class="senateFirstPage">
		<li>
			<div class="clearfix" id="programCourse">
				<div class="UItab">
					<div class="tabLables">
						<span <cfif currentTab eq "labCourse">class="active"</cfif> id="labCourse" tabTarget="Course">计划课程列表</span>
						<span <cfif currentTab eq "labChoosen">class="active"</cfif> id="labChoosen" tabTarget="Choosen">已选课程</span>
						<span <cfif currentTab eq "labCourseTable">class="active"</cfif> id="labCourseTable" tabTarget="CourseTable">个人课表</span>
					</div>
				</div>
				<script language="javascript" type="text/javascript">
					//<![CDATA[
					<!--
					$(document).ready(function() {
						
						/* init Tab */
						$("div.tabContent").hide();
						$("div#" + $("div.tabLables span.active").attr("tabTarget")).show();
						
						$("div.tabLables > span").click(function(){
							var $targetID = $(this).attr("tabTarget");
							
							$("div.tabLables span").removeClass("active");
							$(this).addClass("active");
							
							$("div.tabContent").not('[id="' + $targetID + '"]').hide();
							$("div.tabContent#" + $targetID).show();
						});
						
					});
					
					// -->
					//]]>
				</script>
				<div id="Course" class="tabContent">
                    <cfif rs_course.recordCount GT 0>
						<div class="noticeBlock">
							<h3><cfoutput>#academicYear#-#academicTerm#</cfoutput> 学期计划课程清单</h3>
							<p>下列是当前选课轮次, 您的主修专业根据培养方案计划开设的所有课程.</p>
							<hr/>
						</div>
						<cfelse>
						<div class="systemNotice">
							<h3><span class="img icon16x16 info"></span><cfoutput>#academicYear#-#academicTerm#</cfoutput> 学期</h3>
							<p>没有可选课程</p>
						</div>
					</cfif>
					
                    <cfloop query="rs_course">
						<div class="courseShow clearfix" cid="<cfoutput>#rs_course.cid#</cfoutput>" prop="<cfoutput>#rs_course.prop_id#</cfoutput>">
							<div class="UIChooser">
								<div class="title">
									<cfif arrayFind(courseArrayDisable, rs_course.cid)>
										<span class="stat locked"></span>
										<cfelseif arrayFind(courseArrayChoosen, rs_course.cid)>
										<a cid="<cfoutput>#rs_course.cid#</cfoutput>" class="trigger close checked"></a>
										<cfelse>
										<a cid="<cfoutput>#rs_course.cid#</cfoutput>" class="trigger close"></a>
									</cfif>
									<span class="name"><cfoutput>#rs_course.course_name#</cfoutput></span>
									<span class="numeric"><cfoutput>#numberFormat(rs_course.course_credit, "_.__")#</cfoutput></span>
									<span>学分</span>
									<span class="type t<cfoutput>#rs_course.prop_id#</cfoutput>"><cfoutput>#rs_course.prop_name#</cfoutput></span>
								</div>
								<div class="courseDesc hidden_elem" cid="<cfoutput>#rs_course.cid#</cfoutput>">
									
									<span>总学时</span> <span class="numeric"><cfoutput>#rs_course.period#</cfoutput></span>
									<cfif rs_course.period_theory gt 0><span>理论学时</span> <span class="numeric"><cfoutput>#rs_course.period_theory#</cfoutput></span></cfif>
									<cfif rs_course.period_practice gt 0><span>实践学时</span> <span class="numeric"><cfoutput>#rs_course.period_practice#</cfoutput></span></cfif>
									<cfif rs_course.period_computer gt 0><span>上机学时</span> <span class="numeric"><cfoutput>#rs_course.period_computer#</cfoutput></span></cfif>
									<cfif rs_course.intro neq "-"><p><span class="name">课程介绍</span><cfoutput>#stringAdvice.XHTMLParagraphFormat(rs_course.intro)#</cfoutput></p></cfif>
									<cfif rs_course.reference_books neq "-"><p><span class="name">参考书籍</span><cfoutput>#stringAdvice.XHTMLParagraphFormat(rs_course.reference_books)#</cfoutput></p></cfif>
								</div>
								<div class="taskList hidden_elem" cid="<cfoutput>#rs_course.cid#</cfoutput>"></div>
							</div>
						</div>
					</cfloop>
					
					<script language="javascript" type="text/javascript">
						//<![CDATA[
						<!--
						
						function chooseCourse(courseId, courseIdx, termId, taskType, taskId, hash) {
							
							/* 课程性质 */
							var courseProp = $("div.courseShow[cid='" + courseId + "']").attr('prop');
							
							/* 显示阻挡信息 */
							$.blockUI({ 
										message: '添加所选课程...',
										css: { 
												'width': '16%',
												'left': '42%',
												'border': 'none', 
												'padding': '15px 5px', 
												'backgroundColor': '#F60', 
												'border-radius': '4px', 
												'opacity': .8, 
												'color': '#fff',
												'cursor': null
												},
										overlayCSS: {
												'backgroundColor': '#000', 
												'opacity': .1 ,
												'cursor': null
												}
										}); 
							
							/* 异步提交选课数据 */
							$.post(
									"<cfoutput>#buildURL('apiChoosePlanTask')#</cfoutput>", 
									{
										'CT' : '<cfoutput>#turnId#</cfoutput>',
										'TID' : '<cfoutput>#termId#</cfoutput>',
										'CID' : courseId,
										'CIDX' : courseIdx,
										'TSK': taskId,
										'TT' : 'P',
										'ST' : hash, 
										'CP' : courseProp,
										'seed': (new Date()).getTime()
									}, 
									function(data) {
										
										//$("#out").html(data);
										result = $.parseJSON(data);
										
										if ( result.success ) {
										
											$("a.trigger[cid='" + courseId + "']").removeClass("open").addClass("close").addClass("checked");
										
											/* 显示其他课程的div */
											$("div.courseShow[cid!='" + courseId + "']").show();

											$("div.courseDesc").addClass("hidden_elem");
											$("div.taskList").addClass("hidden_elem").html("");
											
											/* 更新学生课表 */
											$.post(
													"<cfoutput>#buildURL('apiChoosenCourseTable')#</cfoutput>", 
													{
														'TID' : '<cfoutput>#termId#</cfoutput>',
														'seed': (new Date()).getTime()
													},
													function(tableData) {
													
														$("#choosenCourseTable").html(tableData);
																												
													});
											
											/* 更新学生选课记录 */
											$.post(
													"<cfoutput>#buildURL('apiChoosenTaskTable')#</cfoutput>", 
													{
														'TID' : '<cfoutput>#termId#</cfoutput>',
														'seed': (new Date()).getTime()
													},
													function(tableData) {
													
														$("#choosenTable").html(tableData);
														
														/* 取消阻挡 */
														$.unblockUI();
														
													});
											
											
											
											
											
										}
										else {
											alert(result.reason);
											/* 取消阻挡 */
											$.unblockUI();
										}
										
										
												
									});
							
							
						}
						
						$(document).ready(function() {
							
							$("a.trigger[cid]").click(function(){
								
								var cid = $(this).attr("cid");

								var status = $(this).hasClass("close");

								var newStatus = true;
								
								/* 切换状态数据 */
								if (status) {
									newStatus = false;
								}
								
								if (newStatus) {
									/* 如果已展开 则 收拢 */
									$(this).removeClass("open").addClass("close");
									
									/* 显示其他课程的div */
									$("div.courseShow[cid!='" + cid + "']").show();

									$("div.courseDesc").addClass("hidden_elem");
									$("div.taskList").addClass("hidden_elem").html("");

								}
								else{
									/* 如果已收拢 则 展开 */
									$(this).removeClass("close").addClass("open");

									/* 隐藏其他课程的div */
									$("div.courseShow[cid!='" + cid + "']").hide();
									
									$("div.courseDesc[cid='" + cid + "']").removeClass("hidden_elem");
									
									$("div.taskList[cid='" + cid + "']").removeClass("hidden_elem").html("正在读取可选任务...");
									
									/* 显示阻挡信息 */
									$.blockUI({ 
												message: '正在读取开班信息...',
												css: { 
														'width': '16%',
														'left': '42%',
														'border': 'none', 
														'padding': '15px 5px', 
														'backgroundColor': '#000', 
														'border-radius': '4px', 
														'opacity': .8, 
														'color': '#fff',
														'cursor': null
														},
												overlayCSS: {
														'backgroundColor': '#000', 
														'opacity': .1 ,
														'cursor': null
														}
												}); 
									
									/* 异步读取指定课程教学班信息 */
									$.post(
											"<cfoutput>#buildURL('apiPlanTaskTable')#</cfoutput>", 
											{
												'TID' : '<cfoutput>#termId#</cfoutput>',
												'CID' : cid,
												'seed': (new Date()).getTime()
											}, 
											function(data) {
												
												$("div.taskList[cid='" + cid + "']").html( data );
												
												/* 取消阻挡 */
												$.unblockUI();
												
											});
								}
								
							});
							
						});
						// -->
						//]]>
					</script>
					
				</div>
				
				<div id="Choosen" class="tabContent">
					
					<div class="noticeBlock">
						<h3><cfoutput>#academicYear#-#academicTerm#</cfoutput> 学期 已选课程清单</h3>
						<p>下列是当前选课轮次, 您已选择修读的所有课程清单.</p>
						<hr/>
					</div>
					
					<div id="choosenTable">
						
						<!--- 显示学生选课记录 --->
						<!-- studentChoosenTask -->
						<cfoutput>#event.getArg("choosenCourse")#</cfoutput>
						<!-- /studentChoosenTask -->
						
						<div id="out"></div>
						
						<script language="javascript" type="text/javascript">
							//<![CDATA[
							<!--
							function removeTask(chooserId, courseId, courseIdx, termId, taskType, taskId, hash) {
								
								/* 显示阻挡信息 */
								$.blockUI({ 
											message: '正在删除课程...',
											css: { 
													'width': '16%',
													'left': '42%',
													'border': 'none', 
													'padding': '15px 5px', 
													'backgroundColor': '#090', 
													'border-radius': '4px', 
													'opacity': .8, 
													'color': '#fff',
													'cursor': null
													},
											overlayCSS: {
													'backgroundColor': '#000', 
													'opacity': .1 ,
													'cursor': null
													}
											}); 
								
								/* 异步提交退课数据 */
								$.post(
										"<cfoutput>#buildURL('apiCancelTask')#</cfoutput>", 
										{
											'CT' : '<cfoutput>#turnId#</cfoutput>',
											'TID' : '<cfoutput>#termId#</cfoutput>',
											'CID' : courseId,
											'CIDX' : courseIdx,
											'TSK': taskId,
											'TT' : taskType,
											'ST' : hash,
											'SCC' : chooserId,
											'seed': (new Date()).getTime()
										}, 
										function(data) {
											
											result = $.parseJSON(data);
										
											if ( result.success ) {
												
												/*  */
												$("a.trigger[cid='" + courseId + "']").removeClass("open").addClass("close").removeClass("checked");
										
												/* 显示其他课程的div */
												$("div.courseShow[cid!='" + courseId + "']").show();

												$("div.courseDesc").addClass("hidden_elem");
												$("div.taskList").addClass("hidden_elem").html("");
												
												/* 更新学生课表 */
												$.post(
														"<cfoutput>#buildURL('apiChoosenCourseTable')#</cfoutput>", 
														{
															'TID' : '<cfoutput>#termId#</cfoutput>',
															'seed': (new Date()).getTime()
														},
														function(tableData) {
														
															$("#choosenCourseTable").html(tableData);
																												
														});
												
												/* 更新学生选课记录 */
												$.post(
														"<cfoutput>#buildURL('apiChoosenTaskTable')#</cfoutput>", 
														{
															'TID' : '<cfoutput>#termId#</cfoutput>',
															'seed': (new Date()).getTime()
														},
														function(tableData) {
													
															$("#choosenTable").html(tableData);
														
															/* 取消阻挡 */
															$.unblockUI();
														
														});
												
											}
											else {
												alert(result.reason);
												/* 取消阻挡 */
												$.unblockUI();
											}
											
										});
								
							}
							// -->
							//]]>
						</script>
					</div>
				</div>
				
				<div id="CourseTable" class="tabContent">
					
					<div class="noticeBlock">
						<h3><cfoutput>#academicYear#-#academicTerm#</cfoutput> 学期 个人课表</h3>
						<p>下列是当前选课轮次, 您已选择修读的课程的上课安排.</p>
						<hr/>
					</div>

					<div id="choosenCourseTable">
						<!--- 显示学生课表 --->
						<!-- studentCourseTable -->
						<cfoutput>#event.getArg("choosenCourseTable")#</cfoutput>
						<!-- /studentCourseTable -->
					</div>
					
				</div>
				
			</div>
		</li>
	</ul>
</div>

